package top.ochiamalu.aioj.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ochiamalu.aioj.common.BaseResponse;
import top.ochiamalu.aioj.common.ErrorCode;
import top.ochiamalu.aioj.domain.dto.set.AddSetDTO;
import top.ochiamalu.aioj.domain.dto.set.DeleteSetDTO;
import top.ochiamalu.aioj.domain.dto.set.PageSetDTO;
import top.ochiamalu.aioj.domain.dto.set.UpdateSetDTO;
import top.ochiamalu.aioj.domain.vo.set.SetDetailVO;
import top.ochiamalu.aioj.domain.vo.set.SetOptionVO;
import top.ochiamalu.aioj.domain.vo.set.SetVO;
import top.ochiamalu.aioj.exception.BusinessException;
import top.ochiamalu.aioj.service.SetService;
import top.ochiamalu.aioj.util.ResultUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 题集控制器
 *
 * @author OchiaMalu
 * @date 2024/12/16
 */
@RestController
@RequestMapping("/sets")
@Slf4j
public class SetController {

    @Resource
    private SetService setService;

    /**
     * 分页题集
     *
     * @param pageSetDTO 页面题集DTO
     * @return {@link BaseResponse }<{@link Page }<{@link SetVO }>>
     */
    @PostMapping("/page")
    public BaseResponse<Page<SetVO>> pageSet(@RequestBody PageSetDTO pageSetDTO) {
        Page<SetVO> setVOPage = setService.pageSet(pageSetDTO);
        return ResultUtils.success(setVOPage);
    }

    /**
     * 添加题集
     *
     * @param addSetDTO 添加set dto
     * @return {@link BaseResponse }<{@link Long }>
     */
    @PostMapping("/add")
    @SaCheckPermission("set:add")
    public BaseResponse<Long> addSet(@RequestBody AddSetDTO addSetDTO) {
        if (StringUtils.isAnyBlank(addSetDTO.getTitle())) {
            log.error("Add set failed due to invalid parameters");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入标题");
        }
        if (addSetDTO.getSetGroupList().isEmpty()) {
            log.error("Add set failed due to no questions");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请添加题目");
        }
        Long setId = setService.addSet(addSetDTO);
        return ResultUtils.success(setId);
    }

    /**
     * 获取主页题集
     *
     * @return {@link BaseResponse }<{@link List }<{@link SetVO }>>
     */
    @GetMapping("/home")
    public BaseResponse<List<SetVO>> listHome() {
        List<SetVO> setVOList = setService.listHome();
        return ResultUtils.success(setVOList);
    }

    /**
     * 题集推荐
     *
     * @return {@link BaseResponse }<{@link List }<{@link SetOptionVO }>>
     */
    @GetMapping("/recommend")
    public BaseResponse<List<SetOptionVO>> listRecommend() {
        List<SetOptionVO> setVOList = setService.listRecommend();
        return ResultUtils.success(setVOList);
    }

    @GetMapping("/{id}")
    public BaseResponse<SetDetailVO> getSetDetail(@PathVariable Long id) {
        if (id == null || id <= 0) {
            log.error("Get set detail failed due to invalid id");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的id");
        }
        SetDetailVO setDetailVO = setService.getSetDetail(id);
        return ResultUtils.success(setDetailVO);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateSet(@RequestBody UpdateSetDTO updateSetDTO) {
        if (updateSetDTO.getId() == null || updateSetDTO.getId() <= 0) {
            log.error("Update set failed due to invalid id");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的id");
        }
        boolean result = setService.updateById(updateSetDTO);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }
        return ResultUtils.success(true);
    }

    /**
     * 删除题集
     *
     * @param deleteSetDTO 删除题集DTO
     * @return {@link BaseResponse }<{@link Boolean }>
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteSet(@RequestBody DeleteSetDTO deleteSetDTO) {
        if (deleteSetDTO.getId() == null || deleteSetDTO.getId() <= 0) {
            log.error("Delete set failed due to invalid id");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请输入正确的id");
        }
        boolean result = setService.deleteSet(deleteSetDTO.getId());
        return ResultUtils.success(result);
    }
}
